package edu.bu.cs673.superkids123.common.DataObjectAccess;

import java.security.GeneralSecurityException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import edu.bu.cs673.superkids123.HibernateUtil;
import edu.bu.cs673.superkids123.SecurityUtil;
import edu.bu.cs673.superkids123.common.model.Parent;

/**
 * 
 * @author Brent Hepburn
 * @Author: Abdelghani Bhalla
 */
public class ParentManager {

	// add Parent
	public static int addParent(String name, String userName, String password,
			String email) {
		int result = -1;
		String encryptedPass = "";
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();
			try {
				encryptedPass = SecurityUtil.encryptProperty(password);
			} catch (GeneralSecurityException e) {
				encryptedPass = password;
			}
			Parent parent = new Parent();
			parent.setName(name);
			parent.setUserName(userName);
			parent.setPassword(encryptedPass);
			parent.setEmail(email);

			result = (Integer) session.save(parent);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}

	// Get Parent by userName and Password
	public static Parent getParentByUsernameAndPassword(String userName,
			String password) {
		Parent parent = new Parent();
		String encryptedPass = "";
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();
			try {
				encryptedPass = SecurityUtil.encryptProperty(password);
			} catch (GeneralSecurityException e) {
				encryptedPass = password;
			}

			Criteria criteria = session.createCriteria(Parent.class);
			criteria.add(Restrictions.eq("userName", userName))
					.add(Restrictions.eq("password", encryptedPass))
					.setMaxResults(1);

			parent = (Parent) criteria.uniqueResult();

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return parent;

	}

	// Get Parent by id
	public static Parent getParentById(int id) {
		Parent parent = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();
			Criteria criteria = session.createCriteria(Parent.class);
			criteria.add(Restrictions.eq("UserID", id)).setMaxResults(1);

			parent = (Parent) criteria.uniqueResult();

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return parent;

	}

	// Delete Parent : we may need when testing.
	public static void deleteParent(Parent parent) {
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			session.delete(parent);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	// Get the number of Parents in the system
	public static Long getParentCount() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			Criteria criteria = session.createCriteria(Parent.class);
			return (Long) criteria.setProjection(Projections.rowCount())
					.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return new Long(0);
	}

	// Add Parent
	public static void addParent(Parent parent) {
		addParent(parent.getName(), parent.getName(), parent.getPassword(),
				parent.getEmail());
	}

	/**
	 * Gets the parents.
	 * 
	 * @return the parents
	 */
	@SuppressWarnings("unchecked")
	public static List<Parent> getParents() {
		List<Parent> parents = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();
			Query query = session.createQuery("From Parent");

			parents = (List<Parent>) query.list();

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return parents;

	}

	// update Parent
	public static void updateParent(Parent parent) {
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();
			try {
				parent.setPassword(SecurityUtil.encryptProperty(parent
						.getPassword()));
			} catch (GeneralSecurityException e) {
				parent.setPassword(parent.getPassword());
			}

			session.update(parent);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

}
